package com.zoffy.ybuhelper;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.SwitchCompat;
import com.zoffy.ybuhelper.dao.CategoryDao;
import com.zoffy.ybuhelper.dao.MenuDao;
import com.zoffy.ybuhelper.dao.OriginDao;
import com.zoffy.ybuhelper.database.YBUDatabase;
import com.zoffy.ybuhelper.pojo.entity.Category;
import com.zoffy.ybuhelper.pojo.entity.Menu;
import com.zoffy.ybuhelper.pojo.entity.Origin;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public class WhatEatActivity extends AppCompatActivity {
    List<Menu> menuList = new ArrayList<>();
    List<Category> categoryList;
    List<Origin> originList;
    CategoryDao categoryDao;
    OriginDao originDao;
    MenuDao menuDao;
    private Spinner originSpinner;
    private Spinner categorySpinner;
    private SwitchCompat choiceSwitch;
    private SwitchCompat randomSwitch;
    private TextView dataNumTextView;

    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_whateat);
        init();


    }
    private void init(){

        dbInit();
        buttonInit();

        spinnerInit();
    }

    private void dbInit() {
        YBUDatabase YBUDatabase = MyApplication.getInstance().getYBUDatabase();
        categoryDao = YBUDatabase.categoryDao();
        originDao = YBUDatabase.originDao();
        menuDao = YBUDatabase.menuDao();
        categoryList=categoryDao.getAll();
        originList=originDao.getAll();
        menuList=menuDao.getUndeletedAll();


        dataNumTextView = findViewById(R.id.data_num);
        dataNumTextView.setText(String.format(Locale.CHINA,"数据池数量: %d",menuList.size()));
    }

    private void spinnerInit() {
        originSpinner = findViewById(R.id.origin_spinner);
        categorySpinner = findViewById(R.id.category_spinner);
        spinnerInitThis(originSpinner, originList.stream().map(Origin::getName).collect(Collectors.toList()));
        spinnerInitThis(categorySpinner, categoryList.stream().map(Category::getName).collect(Collectors.toList()));
    }

    private <T> void spinnerInitThis(Spinner spinner, List<T> list) {
        ArrayAdapter<T> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
        adapter.addAll(list);
        adapter.setDropDownViewResource(androidx.constraintlayout.widget.R.layout.support_simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    private void buttonInit() {
        Button next_eat = findViewById(R.id.next_eat);
        next_eat.setOnClickListener(v -> {
            TextView textMenuView = findViewById(R.id.menu_name);
            TextView textOriginView = findViewById(R.id.origin_name);
            int random = (int) (Math.random() * menuList.size());
            if(menuList.size()==0){
                textMenuView.setText("没有符合条件的菜品");
            }
            else{
                Menu menu=menuList.get(random);
                Origin origin=originDao.get(menu.getOriginId());
                textMenuView.setText(menu.getName());
                textOriginView.setText(origin.getName());
            }

        });

        Button eat_control = findViewById(R.id.eat_control);
        eat_control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(WhatEatActivity.this, EatControlActivity.class);
                startActivity(intent);

            }
        });

        choiceSwitch = findViewById(R.id.choice_switch);
        randomSwitch = findViewById(R.id.random_switch);
        randomSwitchChangeEvent(false);
        choiceSwitchChangeEvent(false);
        choiceSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                choiceSwitchChangeEvent(isChecked);
            }
        });
        randomSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                randomSwitchChangeEvent(isChecked);
            }
        });

        Button saveConfig=findViewById(R.id.data_change);
        saveConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!choiceSwitch.isChecked()){
                    if (originList.size()==0){
                        return;
                    }
                    Origin origin= originList.get(originSpinner.getSelectedItemPosition());
                    menuList=menuDao.getAllByOriginId(origin.getId());
                }
                else{
                    if (categoryList.size()==0){
                        return;
                    }
                    Category category=categoryList.get(categorySpinner.getSelectedItemPosition());
                    menuList=menuDao.getAllByCategoryId(category.getId());
                }
                dataNumTextView.setText(String.format(Locale.CHINA,"数据池数量: %d",menuList.size()));
                Toast.makeText(WhatEatActivity.this,"数据池已变更",Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void randomSwitchChangeEvent(boolean isChecked){
        LinearLayout seniorRandomLinearLayout=findViewById(R.id.senior_random);
        if (!isChecked){
            seniorRandomLinearLayout.setVisibility(View.GONE);
            menuList=menuDao.getUndeletedAll();
            dataNumTextView.setText(String.format(Locale.CHINA,"数据池数量: %d",menuList.size()));
            Toast.makeText(this,"数据池已变更",Toast.LENGTH_SHORT).show();
        }
        else {
            seniorRandomLinearLayout.setVisibility(View.VISIBLE);
        }
    }
    private void choiceSwitchChangeEvent(boolean isChecked){
        LinearLayout categoryLinearLayout=findViewById(R.id.category_area);
        LinearLayout originLinearLayout=findViewById(R.id.origin_area);
        if (!isChecked){
            categoryLinearLayout.setVisibility(View.GONE);
            originLinearLayout.setVisibility(View.VISIBLE);
        }
        else {
            originLinearLayout.setVisibility(View.GONE);
            categoryLinearLayout.setVisibility(View.VISIBLE);
        }
    }
}